package com.hot12345.springmvc.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.hot12345.common.spring.SpringAbstractController;
import com.hot12345.dao.InfoDaoImpl;
import com.hot12345.dao.ItemDaoImpl;
import com.hot12345.dao.SiteDaoImpl;
import com.hot12345.entity.Info;
import com.hot12345.entity.Item;
import com.hot12345.entity.Site;
import com.hot12345.entity.User;

@Controller
@RequestMapping("/admin/site")
public class SiteController extends SpringAbstractController {

	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		SiteDaoImpl siteDao = SiteDaoImpl.getInstance();
		pageBefore(request, modelMap);
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("state_gte", 0);
		
		
		List<Site> siteList = siteDao.findByParamsPage(params, getStartNo(modelMap), getPageSize(modelMap));
		long totalNum = siteDao.findCount(params);
		
		pageAfter(request, totalNum, modelMap);
		
		modelMap.put("siteList", siteList);
		
		return new ModelAndView("admin/site/list");
	}
	
	@RequestMapping("/insert")
	public void insert(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			@RequestParam("name") String name,
			@RequestParam("abbreviation") String abbreviation,
			@RequestParam("domain") String domain) throws Exception {
		User loginUser = (User)modelMap.get("loginUser");
		Site site = new Site();
		site.setCerateUserId(loginUser.getId());
		site.setAbbreviation(abbreviation);
		site.setCreateDate(new Date());
		site.setCreateUserName(loginUser.getName());
		site.setDomain(domain);
		site.setName(name);
		site.setState(1);
		
		SiteDaoImpl siteDao = SiteDaoImpl.getInstance();
		siteDao.insert(site);
		
		
		response.sendRedirect("/admin/site/index");
	}
	
	@RequestMapping("/add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
				
		return new ModelAndView("admin/site/siteAdd");
	}
	
	@RequestMapping(value="/edit", method = RequestMethod.GET)
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		SiteDaoImpl siteDao = SiteDaoImpl.getInstance();
		Site site = siteDao.findByPrimaryKey(id);
		modelMap.put("site", site);
		
		return new ModelAndView("admin/site/siteEdit");
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public void update(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id,
			String name,
			String abbreviation,
			String domain) throws Exception {
		
		SiteDaoImpl siteDao = SiteDaoImpl.getInstance();
		Site site = new Site();
		site.setId(id);
		site.setName(name);
		site.setAbbreviation(abbreviation);
		site.setState(1);
		site.setDomain(domain);
		siteDao.update(site);
		
		response.sendRedirect("/admin/site/index");
	}
	
	/**
	 * 冻结
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param id
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-3 下午11:48:23
	 * @version V1.0
	 */

	@RequestMapping(value="/freeze", method = RequestMethod.GET)
	public void freeze(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		SiteDaoImpl siteDao = SiteDaoImpl.getInstance();
		Site site = siteDao.findByPrimaryKey(id);
		site.setState(0);
		siteDao.update(site);
		
		response.sendRedirect("/admin/site/index");
	}
	
	/**
	 * 恢复正常状态
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param id
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-3 下午11:48:43
	 * @version V1.0
	 */
	@RequestMapping(value="/restore", method = RequestMethod.GET)
	public void restore(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		SiteDaoImpl siteDao = SiteDaoImpl.getInstance();
		Site site = siteDao.findByPrimaryKey(id);
		site.setState(1);
		siteDao.update(site);
		
		response.sendRedirect("/admin/site/index");
	}
}
